import datetime
from html import unescape, escape
from exts import db
from passlib.apps import custom_app_context as pwd_context
from itsdangerous import TimedJSONWebSignatureSerializer as Serializer, SignatureExpired, BadSignature
from urllib.parse import urlencode
from app import app


class QTaskHistory(db.Model):
    __tablename__ = 'qiao_task_history'
    id = db.Column(db.String(255), primary_key=True)
    task_id = db.Column(db.String(255), unique=False)
    task_name = db.Column(db.String(255), unique=False)
    func_name = db.Column(db.String(255), unique=False)
    time = db.Column(db.Integer, unique=False)
    span_time = db.Column(db.String(255), unique=False)
    user = db.Column(db.String(255), unique=False)
    status = db.Column(db.Boolean, unique=False)
    log = db.Column(db.TEXT, unique=False)

    def to_dict(self):
        '''将对象转换为字典数据'''
        reDict = {
            "id": self.id,
            "task_id": self.task_id,
            "task_name": self.task_name,
            "func_name": self.func_name,
            "time": self.time,
            "span_time": self.span_time,
            "user": self.user,
            "status": self.status,
            "log": self.log,
        }
        return reDict


class QJobHistory(db.Model):
    __tablename__ = 'qiao_job_history'
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(255), unique=False)
    task_id = db.Column(db.String(255), unique=False)
    func_name = db.Column(db.String(255), unique=False)
    time = db.Column(db.Integer)
    userid = db.Column(db.String(255), unique=False)
    run_times = db.Column(db.String(255), unique=False)
    run_time = db.Column(db.TEXT, unique=False)
    status = db.Column(db.Integer, unique=False)  # 0:创建，-1:：删除，1：暂停，2：恢复，3：编辑

    def to_dict(self):
        '''将对象转换为字典数据'''
        reDict = {
            "id": self.id,
            "name": self.name,
            "task_id": self.task_id,
            "func_name": self.func_name,
            "time": self.time,
            "userid": self.userid,
            "run_times": self.run_times,
            "run_time": self.run_time,
            "status": self.status,
        }
        return reDict
